<template>
  <div v-loading="loading" class="app-container">
    <div class="head-container">
      <searchForm ref="searchForm" :parent="currentObject" />
    </div>
    <div style="padding-top:5px">
      <el-button size="mini" type="danger" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-delete" round @click="doDeleteItem">作废</el-button>
      <el-button type="primary" size="mini" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-upload" round @click="batchActive">激活</el-button>
      <attachmentBnt ref="attachmentBnt" :parent="currentObject" />
      <downloadBnt ref="downloadBnt" :parent="currentObject" />
      <uploadBnt ref="uploadBnt" :parent="currentObject" />
      <el-button type="primary" size="mini" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-circle-plus" round @click="toBatchAdd">批量新增</el-button>
      <el-button type="primary" size="mini" style="float:right;margin-bottom:5px;margin-left:0px;" icon="el-icon-circle-plus" round @click="toAdd">新增</el-button>
      <cancel ref="cancel" :parent="currentObject" />
      <dataGrid ref="dataGrid" :parent="currentObject" />
    </div>
    <activeForm ref="activeForm" :parent="currentObject" />
    <batchAddForm ref="batchAddForm" :parent="currentObject" />
  </div>
</template>

<script>
import activeForm from '@/views/active-item/create/create-active'
import batchAddForm from '@/views/active-item/create/batch-add'
import searchForm from '@/views/active-item/search-form'
import attachmentBnt from '@/views/active-item/file/attachment'
import downloadBnt from '@/views/active-item/file/download'
import uploadBnt from '@/views/active-item/file/upload'
import dataGrid from '@/views/active-item/data-grid'
import initData from '@/utils/initData'
import { parseTime } from '@/utils/index'
import { Message } from 'element-ui'
import { batchActiveItem } from '@/api/active-item'
import { showMessage } from '@/utils/activeItemUtil'
import common from '@/api/active-global'
import cancel from '@/views/active-item/file/cancel'
import { doDeleteItem } from '@/utils/changeItem'

export default {
  components: { activeForm, batchAddForm, searchForm, attachmentBnt, downloadBnt, uploadBnt, dataGrid, cancel },
  mixins: [initData],
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      currentObject: this,
      ids: [],
      loading: false,
      itemStatuses: [
        { value: common.ITEM_STATUS_BUYER_ADD, name: '采购新增' },
        { value: common.ITEM_STATUS_DELETE, name: '已作废' }
      ],
      addOrUpdate: 'addOrUpdateActiveItem',
      changeType: common.itemStatus,
      batchAddItemInfo: 'batchAddActiveItemInfo'
    }
  },
  created() {
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    doDeleteItem,
    beforeInit() {
      this.url = common.basePath + '/queryItemStatusActiveList'
      this.params.pageNum = this.page
      this.params.pageSize = this.size
      return true
    },
    // 详情触发
    toItemInfo(item) {
      const activeForm = this.$refs.activeForm
      activeForm.title = '商品激活-详情页面'
      activeForm.itemInfo = JSON.parse(item.itemInfo)
      activeForm.itemInfo.vendorItemInfo = JSON.parse(item.vendorItemInfo)
      activeForm.itemStatus = item.itemStatus
      activeForm.id = item.id
      activeForm.buyerId = item.buyerId
      activeForm.dialog = true
    },
    toAdd() {
      const activeForm = this.$refs.activeForm
      activeForm.title = '商品激活-新增页面'
      activeForm.dialog = true
    },
    // 批量新增
    toBatchAdd() {
      const batchAddForm = this.$refs.batchAddForm
      batchAddForm.title = '商品激活-批量新增页面'
      batchAddForm.dialog = true
    },
    // 批量激活
    batchActive() {
      if (this.ids.length < 1) {
        Message({
          message: '请选择需要激活的item。',
          type: 'error',
          duration: 3 * 1000
        })
      } else {
        this.$confirm('确认同意选择的 ' + this.ids.length + ' 条item信息?', '批量激活确认', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          batchActiveItem({ ids: this.ids, changeType: this.changeType }).then(res => {
            this.toQuery()
            showMessage(this, res)
            this.loading = false
          }, error => {
            console.log(error)
            this.loading = false
          })
        })
      }
    },
    // 查询
    toQuery() {
      this.page = 0
      this.currentPage = 1
      this.init()
      this.$refs.dataGrid.doClearSelection()
    }
  }
}
</script>
